package com.cacho.s2b.lesson.learn;

import lombok.extern.slf4j.Slf4j;

import java.sql.*;


/**
 * @Description java连接mysql数据库
 * @Author LingWei
 * @date 2023/12/31
 **/
@Slf4j
public class LearnMysql {
    public static void main( String[] args ) {
        // JDBC驱动程序名称及URL
        String driver = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/cacho_stock";
        // MySQL配置信息
        String username = "root";
        String password = "123456";
        Connection conn;
        Statement stmt;
        ResultSet rs;
        try {
            //Class.forName(driver);  // 可以不需要了
            conn = DriverManager.getConnection(url,username,password);
            // 创建Statement对象
            stmt = conn.createStatement();
            // 查询语句
            String sql = "SELECT * FROM china_stock";
            // 执行查询
            rs = stmt.executeQuery(sql);
            // 处理结果集
            while (rs.next()) {
                String code = rs.getString("stock_no");
                String name = rs.getString("stock_name");

                // 输出结果
                System.out.println("code: " + code);
                System.out.println("stock_name: " + name);
            }
        } catch (SQLException s) {
            log.warn("数据库查询异常信息：{}",s.getMessage());
        }
    }
}
